<template>
    <div class="table">
        <el-breadcrumb separator="/" class="breadCrumb">
            <el-breadcrumb-item>{{$t('sidebar.controlAndAlert')}}</el-breadcrumb-item>
            <el-breadcrumb-item>{{$t('sidebar.taskAlertInfoNotify')}}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="crumbs">
            <el-row :gutter="15">
                <el-col :span="2" style="min-width: 100px">
                    <el-input
                        :placeholder="$t('taskAlertInfoNotify.pleaseInput',{ name: 'ID' })"
                        v-model="searchForm.id">
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-input
                        :placeholder="$t('taskAlertInfoNotify.pleaseInput',{ name: $t('taskAlertInfoNotify.taskClass') })"
                        v-model="searchForm.taskClass">
                    </el-input>
                </el-col>
                <el-col :span="6" style="min-width:345px;">
                    <el-date-picker
                        v-model="queueDate"
                        type="datetimerange"
                        :picker-options="pickerOptions"
                        :placeholder="$t('taskAlertInfoNotify.pleaseInput',{ name: $t('taskAlertInfoNotify.timeSlot') })" style="width:100%">
                    </el-date-picker>
                </el-col>
                <el-col :span="4" style="line-height: 35px;">
                    <el-button size="small" type="primary" @click="searchData()">{{$t('taskAlertInfoNotify.search')}}</el-button>
                    <el-button size="small" type="primary" @click="refreshData()">{{$t('taskAlertInfoNotify.refresh')}}</el-button>
                </el-col>

            </el-row>
        </div>
        <el-table :data="tableData" border style="width: 100%" :max-height="$store.state.allTable.tableHeight" v-loading="tableDataLoading"
                  :element-loading-text="$t('allTable.dataLoading')">
            <el-table-column prop="id" label="ID" width="60">
            </el-table-column>
            <el-table-column prop="alertInfoId" :label="$t('taskAlertInfoNotify.alertInfoId')" min-width="150">
            </el-table-column>
            <el-table-column prop="contactMan" :label="$t('taskAlertInfoNotify.contactMan')" width="120">
            </el-table-column>
            <el-table-column prop="contactInfoType" :label="$t('taskAlertInfoNotify.contactInfoType')" min-width="150">
            </el-table-column>
            <el-table-column prop="contactInfo" :label="$t('taskAlertInfoNotify.contactInfo')" width="200">
            </el-table-column>
            <el-table-column prop="taskClass" :label="$t('taskAlertInfoNotify.taskClass')" min-width="150">
            </el-table-column>
            <el-table-column prop="createDate" :label="$t('taskAlertInfoNotify.createDate')" width="150">
            </el-table-column>
            <el-table-column prop="sendedDate" :label="$t('taskAlertInfoNotify.sendedDate')" width="150">
            </el-table-column>
            <el-table-column prop="sendTimes" :label="$t('taskAlertInfoNotify.sendTimes')" min-width="100">
            </el-table-column>
            <el-table-column prop="status" :label="$t('taskAlertInfoNotify.status')" min-width="80" :formatter="statusShow">
            </el-table-column>
            <!--<el-table-column :label="$t('taskAlertInfoNotify.operation')" width="100">
                <template scope="scope">
                    <el-button size="mini"
                               @click="handleEdit(scope)">{{$t('taskAlertInfoNotify.edit')}}</el-button>
                    <el-button size="mini" type="danger"
                               @click="deleteData(scope.row.id)">{{$t('taskAlertInfoNotify.delete')}}</el-button>
                </template>
            </el-table-column>-->
        </el-table>
        <div class="mgtb20" :class="{ pagination : tableData.length !== 0}">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change ="handleCurrentChange"
                :page-sizes="$store.state.allTable.pageSizesChoice"
                :page-size="pageSizeGetter"
                layout="total, sizes, prev, pager, next, jumper"
                :total="sizeAll"
                :page-count="pageCount"
                v-if="tableData.length !== 0">
            </el-pagination>

        </div>
        <el-dialog :title="$t('sidebar.taskAlertInfoNotify')" size="mini" v-model="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" :inline="true" label-position="right">
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskAlertInfoNotify.alertInfoId')" prop="alertInfoId">
                            <el-input v-model="ruleForm.alertInfoId" :placeholder="$t('taskAlertInfoNotify.alertInfoId')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item :label="$t('taskAlertInfoNotify.taskClass')" prop="taskClass">
                            <el-input v-model="ruleForm.taskClass" :placeholder="$t('taskAlertInfoNotify.taskClass')" :readonly="!hasTaskClass"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskAlertInfoNotify.contactMan')" prop="contactMan">
                            <el-input v-model="ruleForm.contactMan" :placeholder="$t('taskAlertInfoNotify.contactMan')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item :label="$t('taskAlertInfoNotify.contactInfo')" prop="contactInfo">
                            <el-input v-model="ruleForm.contactInfo" :placeholder="$t('taskAlertInfoNotify.contactInfo')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskAlertInfoNotify.contactInfoType')" prop="contactInfoType">
                            <!--<el-input v-model="ruleForm.contactInfoType" :placeholder="$t('taskAlertInfoNotify.contactInfoType')"></el-input>-->
                            <el-select v-model="ruleForm.contactInfoType" clearable :placeholder="$t('taskAlertInfoNotify.contactInfoType')">
                                <el-option key="mobile" label="mobile" value="mobile"></el-option>
                                <el-option key="qq" label="qq" value="qq"></el-option>
                                <el-option key="wx" label="wx" value="wx"></el-option>
                                <el-option key="email" label="email" value="email"></el-option>

                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!--<el-col>
                        <el-form-item :label="$t('taskAlertInfoNotify.sendedDate')" prop="sendedDate">
                            <el-input v-model="ruleForm.sendedDate" :placeholder="$t('taskAlertInfoNotify.sendedDate')"></el-input>
                        </el-form-item>
                    </el-col>-->
                </el-row>
                <!--<el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskAlertInfoNotify.sendTimes')" prop="sendTimes">
                            <el-input v-model="ruleForm.sendTimes" :placeholder="$t('taskAlertInfoNotify.sendTimes')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col></el-col>
                </el-row>-->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">{{$t('taskAlertInfoNotify.cancel')}}</el-button>
                <el-button type="primary" @click="addData('ruleForm')">{{$t('taskAlertInfoNotify.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import { mapGetters,mapMutations } from 'vuex';
    import common from '../../assets/script/common';
    export default {
        data() {
            return {
                tableData:[],
                pageName:'taskAlertContact',
                pageCount:1,
                sizeAll:0,
                dialogFormVisible: false,
                queueDate:[],
                searchForm:{
                    id:'',
                    taskClass:'',
                    startCreateDate:'',
                    endCreateDate:''
                },
                ruleForm: {
                    id:'',
                    alertInfoId: '',
                    contactInfo: '',
                    contactInfoType: '',
                    contactMan:'',
                    createDate:'',
                    sendTimes:'',
                    sendedDate:'',
                    status:'',
                    taskClass: ''
                },
                rules: {
                    alertInfoId: [
                        { required: true, message: this.$i18n.t('taskAlertInfoNotify.pleaseInput',{ name: this.$i18n.t('taskAlertInfoNotify.alertInfoId') }), trigger: 'blur' }
                    ],
                    contactInfo: [
                        { required: true, message: this.$i18n.t('taskAlertInfoNotify.pleaseInput',{ name: this.$i18n.t('taskAlertInfoNotify.contactInfo') }), trigger: 'blur' }
                    ],
                    contactMan: [
                        { required: true, message: this.$i18n.t('taskAlertInfoNotify.pleaseInput',{ name: this.$i18n.t('taskAlertInfoNotify.contactMan') }), trigger: 'blur' }
                    ],
                    contactInfoType: [
                        { required: true, message: this.$i18n.t('taskAlertInfoNotify.pleaseInput',{ name: this.$i18n.t('taskAlertInfoNotify.contactInfoType') }), trigger: 'blur' }
                    ],
                    sendTimes: [
                        { required: true, message: this.$i18n.t('taskAlertInfoNotify.pleaseInput',{ name: this.$i18n.t('taskAlertInfoNotify.sendTimes') }), trigger: 'blur' }
                    ],
                    sendedDate: [
                        { required: true, message: this.$i18n.t('taskAlertInfoNotify.pleaseInput',{ name: this.$i18n.t('taskAlertInfoNotify.sendedDate') }), trigger: 'blur' }
                    ],
                    taskClass: [
                        { required: true, message: this.$i18n.t('taskAlertInfoNotify.pleaseInput',{ name: this.$i18n.t('taskAlertInfoNotify.taskClass') }), trigger: 'change' }
                    ]
                },
                hasTaskClass: true,
                dialogType:'',
                searchId:'',
                tableDataLoading:true,
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                }
            }
        },
        created() {
            this.getData(1,this.$store.state.allTable.pageSize);
        },
        computed:{
            ...mapGetters([
                "pageSizeGetter",
            ]),
        },
        methods:{
            ...mapMutations([
                'pageSizeMutation'
            ]),
            handleSizeChange(val) {
                let self = this;
                self.pageSizeMutation(val);
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            handleCurrentChange(val) {
                common.changePageGoToTop(this,val);
            },
            getData(_pageSize,_pageNum) {
                let self = this;
                self.tableDataLoading = true;
                common.getTableList(this, 'taskAlertInfoNotify', _pageSize, _pageNum, self.searchForm, (data) => {
                    self.tableData = data.results ? data.results : [];
                    self.pageCount = data.pageCount;
                    self.sizeAll = data.sizeAll;
                    self.tableDataLoading = false;
                });
            },
            addData(formName) {
                let self = this;
                let __token = sessionStorage.getItem("token");
                let __ruleForm = this.ruleForm;

                this.$refs[formName].validate((valid) => {
                    if(valid){
                        if(this.dialogType === 'add'){
                            common.postTableList(self, 'taskAlertInfoNotify', __ruleForm, () => {
                                self.getData(1,self.$store.state.allTable.pageSize);
                                this.dialogFormVisible = false;
                                this.hasTaskClass = true;
                                for(let key in __ruleForm){
                                    __ruleForm[key] = '';
                                }
                                this.ruleForm = __ruleForm;
                            });
                        }else if(this.dialogType === 'edit'){
                            common.putTableList(self, 'taskAlertInfoNotify', __ruleForm, () => {
                                self.getData(1,self.$store.state.allTable.pageSize);
                                this.dialogFormVisible = false;
                                this.hasTaskClass = true;
                                for(let key in __ruleForm){
                                    __ruleForm[key] = '';
                                }
                                this.ruleForm = __ruleForm;
                            });
                        }

                    }
                });
            },
            deleteData(_id) {
                let self = this;
                common.deleteTableList(self, 'taskAlertInfoNotify', _id, () => {
                    self.getData(1,self.$store.state.allTable.pageSize);
                })
            },
            handleEdit(scopes) {
                let __ruleForm = this.ruleForm;
                this.hasTaskClass = false;
                for(let key in __ruleForm){
                    if(scopes.row[key] !== null){
                        __ruleForm[key] = scopes.row[key].toString();
                    }else {
                        __ruleForm[key] = '';
                    }

                }
                this.ruleForm = __ruleForm;
                this.dialogFormVisible = true;
                this.dialogType = 'edit';
            },
            searchData() {
                let self = this;
                if(self.queueDate[0] !== null && self.queueDate[1] !== null) {
                    self.searchForm.startCreateDate = self.queueDate[0];
                    self.searchForm.endCreateDate = self.queueDate[1];
                }else{
                    self.searchForm.startRunDate = '';
                    self.searchForm.endRunDate = '';
                }

                self.getData(1,self.$store.state.allTable.pageSize);
            },
            refreshData() {
                let self = this;
                for(let key in self.searchForm){
                    self.searchForm[key] = '';
                }
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            addButton() {
                let __ruleForm = this.ruleForm;
                for(let key in __ruleForm){
                    __ruleForm[key] = '';
                }
                this.dialogFormVisible = true;
                this.dialogType = 'add';
                this.hasTaskClass = true;
            },
            statusShow(row, column) {
                if(row.status === 1)
                    return "正常";
            }
        }
    }
</script>
<style lang="stylus">

</style>
